AddCSLuaFile("shared.lua")
AddCSLuaFile("cl_init.lua")

include("shared.lua")

function ENT:Initialize()
	self.Entity:PhysicsInit(SOLID_VPHYSICS)
	self.Entity:SetMoveType( MOVETYPE_VPHYSICS )
	self.Entity:SetTrigger( true )
	self.Entity:SetSolid(SOLID_VPHYSICS)
	self.Entity:SetUseType(SIMPLE_USE)

	local phys = self.Entity:GetPhysicsObject()
	if phys:IsValid() then 
	phys:Wake() 
    phys:SetMass( math.ceil( phys:GetVolume()/500)*10 )
	end
    
    self.area = math.pow(self.Entity:BoundingRadius()*3.14/2,2)
    
    self.Core = NULL
    self.steamvent = 0
    self.thrust = 0
    self.forward = 0
    self.back = 0
    
    self.maxhp = 100
    self.hp = self.maxhp
    self.repair = 1
    self.Entity:SetNetworkedFloat("repair", self.repair)
    
    self.cost = math.ceil(self.area/500)
    self.Repay = 1
    Ent_InitCost( self.Entity )
    
    local max = self.Entity:OBBMaxs()
    local min = self.Entity:OBBMins()
    local width = math.abs(max.x-min.x)
    local height = math.abs(max.y-min.y)
    local depth = math.abs(max.z-min.z)
    
    if(width<height and width<depth) then
        self.Norm = Vector(1,0,0)
    elseif(height<width and height<depth) then
        self.Norm = Vector(0,1,0)
    else
        self.Norm = Vector(0,0,1)
    end
    
    if(self.Entity:GetModel()=="models/props_phx/misc/propeller2x_small.mdl") then self.Norm = Vector(0,0,1) end
    
    /*
    local display = ents.Create("prop_physics")
    display:SetModel("models/hunter/blocks/cube025x025x025.mdl")
    display:SetPos( self.Entity:LocalToWorld(self.Norm*100) )
    display:SetParent( self.Entity )
    */
    
	self.Inputs = Wire_CreateInputs(self, { "Forward", "Back" })

end

function ENT:TriggerInput(iname, value)
	if (iname == "Forward") then
		self.forward = math.Clamp(value,-1,1)
	elseif (iname == "Back") then
        self.back = math.Clamp(value,-1,1)
	end
end
    
function ENT:Think()
    
    local prevrepair = self.repair
    self.repair = math.min(self.repair+0.002,1)
    if(prevrepair==self.repair) then /*comment*/ else self.Entity:SetNetworkedFloat("repair", self.repair) end
    
    local phys = self.Entity:GetPhysicsObject()
    if( phys:IsValid() and self.Core:IsValid() ) then
    
        local maxforce = math.Clamp( self.area/2.5 ,-50000,50000)*self.repair*self.Core.pressure
        self.thrust = (self.thrust*19 + (self.forward-self.back))/20
        self.Entity:SetNetworkedFloat("thrust",self.thrust)
        local force =  maxforce*self.thrust
        
        local vec = Vector( self.Norm.x,self.Norm.y,self.Norm.z )
            vec:Rotate(self.Entity:GetAngles())
        phys:ApplyForceCenter( -force*vec )
        
        self.steamvent = math.abs((force)/2000)
        
        self.Entity:NextThink( CurTime() + 0.02 )
    else
        self.Entity:NextThink( CurTime() + 1 )
    end
    
    return true
end

function ENT:OnRemove()
	RemoveRepay( self )
end

function ENT:OnRestore()
if WireLib then WireLib.Restored(self) end
end

function ENT:PreEntityCopy()
    if WireLib then
    local DupeInfo = WireLib.BuildDupeInfo(self)
        if DupeInfo then
            duplicator.StoreEntityModifier( self, "WireDupeInfo", DupeInfo )
        end
    end
end

function ENT:PostEntityPaste( Player, Ent, CreatedEntities )
    Ent.Owner = Player
    Ent_InitCost( self.Entity )
    if WireLib and (Ent.EntityMods) and (Ent.EntityMods.WireDupeInfo) then
        WireLib.ApplyDupeInfo(Player, Ent, Ent.EntityMods.WireDupeInfo, function(id) return CreatedEntities[id] end)
    end
end